package com.pomr.util.communication.cx215;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import com.alibaba.fastjson.JSONObject;
import com.pomr.util.communication.Communication;
import com.pomr.util.communication.cx211.CX211Back;

public class CX215Back implements Communication,Serializable{
	public int getCode() {
		return code;
	}


	public void setCode(int code) {
		this.code = code;
	}


	public String getCodeName() {
		return codeName;
	}


	public void setCodeName(String codeName) {
		this.codeName = codeName;
	}


	public int getCardCount() {
		return cardCount;
	}


	public void setCardCount(int cardCount) {
		this.cardCount = cardCount;
	}


	public List<CX215BackBase> getCardList() {
		return cardList;
	}


	public void setCardList(List<CX215BackBase> cardList) {
		this.cardList = cardList;
	}


	private int code;//查询状态
	private String codeName;
	private int cardCount;//卡片数
	private List<CX215BackBase> cardList;
	
	public static Object fromJson(String jsonStr) {
//		jsonStr = "{"+
//"'cardCount':3,"+
//"'cardList':["+
//"{"+
//"'accountName': '18923704401',"+
//"'activityTime': '2017-05-12',"+
//"'cardPackage':"+
//"'%E8%81%94%E9%80%9A_1000M%2F%E6%9C%88_%E5%8C%85%E5%B9%B4',"+
//"'cardType': '%E7%BA%AFmicro',"+
//"'code': 0,"+
//"'expirationTime': '2018-04-26',"+
//"'firstActivityTime': '2017-05-12',"+
//"'flowExpriationTime': '2018-06-26',"+
//"'iccid': '89860616090048928111',"+
//"'imsi': '460069059003141',"+
//"'msisdn': '861064699433340',"+
//"'operator': 2,"+
//"'outdata': 314.09,"+
//"'packageCode': 4565,"+
//"'status': 1,"+
//"'totaldata': 1400,"+
//"'updateTime': '2017-09-18 17:47:38'"+
//"},"+
//"{"+
//"'accountName': '18923704401',"+
//"'activityTime': '2017-05-12',"+
//"'cardPackage':"+
//"'%E8%81%94%E9%80%9A_1000M%2F%E6%9C%88_%E5%8C%85%E5%B9%B4',"+
//"'cardType': '%E7%BA%AFmicro',"+
//"'code': 0,"+
//"'expirationTime': '2018-04-26',"+
//"'firstActivityTime': '2017-05-12',"+
//"'flowExpriationTime': '2018-06-26',"+
//"'iccid': '89860616090048928112',"+
//"'imsi': '460069059003142',"+
//"'msisdn': '861064699433341',"+
//"'operator': 2,"+
//"'outdata': 314.09,"+
//"'packageCode': 4565,"+
//"'status': 1,"+
//"'totaldata': 1400,"+
//"'updateTime': '2017-09-18 17:47:38'"+
//"}],"+
//"'code':0"+
//"}";
		CX215Back back = (CX215Back)JSONObject.parseObject(jsonStr, CX215Back.class);
		//查询状态
		if(back.getCode() == 0){
			back.setCodeName("查询正常");
		}else if(back.getCode() == 1){
			back.setCodeName("appid、secret、sign有误");
		}else if(back.getCode() == 2){
			back.setCodeName("imsi、iccid、msisdn有误");
		}else if(back.getCode() == 9999){
			back.setCodeName("意外错误");
		}
		if(back.getCardCount() > 0){
			List<CX215BackBase> list = back.getCardList();
			for(int i=0; i<list.size(); i++){
				CX215BackBase base = list.get(i);
				//查询状态
				if(base.getCode() == 0){
					base.setCodeName("查询正常");
				}else if(back.getCode() == 1){
					base.setCodeName("appid、secret、sign有误");
				}else if(back.getCode() == 2){
					base.setCodeName("imsi、iccid、msisdn有误");
				}else if(back.getCode() == 9999){
					base.setCodeName("意外错误");
				}
				
				if(base.getOperator() == 1){
					base.setOperatorName("移动");
				}else if(base.getOperator() == 2){
					base.setOperatorName("联通");
				}else if(base.getOperator() == 3){
					base.setOperatorName("电信");
				}
				
				//卡片状态
				if(base.getStatus() == 1){
					base.setStatusName("激活");
				}else if(base.getStatus() == 2){
					base.setStatusName("可激活");
				}else if(base.getStatus() == 3){
					base.setStatusName("测试");
				}else if(base.getStatus() == 4){
					base.setStatusName("停用");
				}else if(base.getStatus() == 4){
					base.setStatusName("作废");
				}
				//剩余流量
				if(base.getTotaldata() == null || base.getTotaldata().trim().equals("")){
					base.setSurplusdata("0");
				}
				else if(base.getTotaldata() == null || base.getTotaldata().trim().equals("")){
					BigDecimal b1 = new BigDecimal(base.getTotaldata());  
					BigDecimal b2 = new BigDecimal(0);  
					base.setSurplusdata(b1.subtract(b2).setScale(2, BigDecimal.ROUND_HALF_UP).toString());
				}else{
					BigDecimal b1 = new BigDecimal(base.getTotaldata());  
					BigDecimal b2 = new BigDecimal(base.getOutdata()); 
					base.setSurplusdata(b1.subtract(b2).setScale(2, BigDecimal.ROUND_HALF_UP).toString());
				}
				//TODO 当前卡片规格 主套餐名称需要解码 
			}
		}
		return back;
	}

}
